/*
** Copyright (C) 2011 Viens Yann
**
** This program is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License as published by the Free Software Foundation; either
** version 2.1 of the License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public
** License along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include <cstdlib>
#include "Variables.hpp"
#include "Object2D.hpp"

Object2D::Object2D(): _pos(-1, -1), _dir(-1, -1), _size(-1, -1)
{
}

Object2D::Object2D(const int posX, const int posY, const int dirX, const int dirY, const int sizeX, const int sizeY):
  _pos(posX, posY),
  _dir(dirX, dirY),
  _size(sizeX, sizeY)
{
}

Object2D::Object2D(Coord2I const& pos, Coord2I const& dir, Coord2I const& size): _pos(pos), _dir(dir), _size(size)
{
}


Object2D::~Object2D()
{
}

void		Object2D::setPos(Coord2I const& pos)
{
  _pos = pos;
}

void		Object2D::setPos(const int x, const int y)
{
  _pos._x = x;
  _pos._y = y;
}

Coord2I const&	Object2D::getPos() const
{
  return _pos;
}


void		Object2D::setDir(Coord2I const& dir)
{
  _dir = dir;
}

void		Object2D::setDir(const int x, const int y)
{
  _dir._x = x;
  _dir._y = y;
}

Coord2I const&	Object2D::getDir() const
{
  return _dir;
}

Coord2I&	Object2D::getDir()
{
  return _dir;
}

void		Object2D::setSize(Coord2I const& size)
{
  _size = size;
}

void		Object2D::setSize(const int x, const int y)
{
  _size._x = x;
  _size._y = y;
}

Coord2I const&	Object2D::getSize() const
{
  return _size;
}

void			Object2D::move()
{
  _pos = _pos + _dir;
}

bool			Object2D::checkCollision(Object2D const& obj) const
{
  Coord2I const&	pos = obj.getPos();
  Coord2I const&	size = obj.getSize();

  if (_pos._x + _size._x < pos._x ||
      _pos._x > pos._x + size._x ||
      _pos._y + _size._y < pos._y ||
      _pos._y > pos._y + size._y)
    return false;
  return true;
}

void			Object2D::rebounds(Object2D& obj, const int modifX, const int modifY)
{
  Coord2I const&	pos = obj.getPos();
  Coord2I const&	size = obj.getSize();

  if (_pos._x >= pos._x && _pos._x <= pos._x + size._x &&
      _pos._y > pos._y && _pos._y + _size._y < pos._y + size._y)
    {
      _dir._x = _dir._x * -1 + modifX;
      _dir._y += modifY;
    }
  else if (_pos._x + _size._x >= pos._x && _pos._x + _size._x <= pos._x + size._x &&
      _pos._y > pos._y && _pos._y + _size._y < pos._y + size._y)
    {
      _dir._x = _dir._x * -1 + modifX;
      _dir._y += modifY;
    }
  else if (_pos._y >= pos._y && _pos._y <= pos._y + size._y &&
      _pos._x > pos._x && _pos._x + _size._x < pos._x + size._x)
    {
      _dir._y = _dir._y * -1 + modifY;
      _dir._x += modifX;
    }
  else if (_pos._y + _size._y >= pos._y && _pos._y + _size._y <= pos._y + size._y &&
      _pos._x > pos._x && _pos._x + _size._x < pos._x + size._x)
    {
      _dir._y = _dir._y * -1 + modifY;
      _dir._x += modifX;
    }
}
